package com.chb.service.impl;

import com.chb.dao.BranchGoodsDao;
import com.chb.dao.InLibrayDao;
import com.chb.dao.StorageVolumeDao;
import com.chb.model.entity.*;
import com.chb.model.vo.BranchAllGoodsVo;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.BranchGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/9/27 9:21
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class BranchGoodsServiceImpl implements BranchGoodsService {
    @Autowired
    private BranchGoodsDao branchGoodsDao;
    @Autowired
    private HttpSession session;
    @Autowired
    private InLibrayDao inLibrayDao;
    @Autowired
    private StorageVolumeDao storageVolumeDao;

    /**
     * @param page     当前页数
     * @param pageSize 每页展示最大数量
     * @create by: 陈海彬
     * @description: 查询入库信息中新增入库所需的信息
     * @create time: 2023/9/27 9:24
     */
    @Override
    public Result selectBranchGoodsById(Integer page, Integer pageSize) {
        //1.获取session
        Staff staff = (Staff) session.getAttribute("staff");
        //2.获取分页参数
        PageHelper.startPage(page, pageSize);
        //3.进行查询
        List<BranchGoods> inLibrayByBranchid = branchGoodsDao.findInLibrayByBranchid(staff.getBranchid());
        //4.返回分页数据
        PageInfo<BranchGoods> pageInfo = new PageInfo<>(inLibrayByBranchid);
        //5.获得PageBean
        PageBean pageBean = new PageBean(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    /**
     * @param id 入库id
     * @create by: 陈海彬
     * @description: 审核通过添加商品信息
     * @create time: 2023/9/27 16:28
     */
    @Override
    @Transactional
    public Result updateShoping(Integer id) {
        //1.进行修改状态码
        InLibray inLibray = new InLibray();
        inLibray.setStatus(2);
        inLibray.setDates(new Date(System.currentTimeMillis()));
        inLibray.setId(id);
        inLibrayDao.updateStatusById(inLibray);
        //2.通过id找出他所有的商品信息
        List<StorageVolume> volumeInfoById = storageVolumeDao.findStorageVolumeInfoById(id);
        StorageVolume storageVolume = volumeInfoById.get(0);
        //3.找出所有信息的列表
        List<StorageVolume> list = storageVolume.getList();
        for (StorageVolume item : list) {
            //获取添加的数量
            Integer num = item.getNum();

            //4.先通过查询如果没有数据就进行添加
            BranchGoods inLibrayByNow = branchGoodsDao.findInLibrayByNow(item);
            //5.判断是否有值
            if (inLibrayByNow != null) {
                //拿到原来的数量并加上要添加的数量
                Integer counts = inLibrayByNow.getCounts() + num;
                inLibrayByNow.setCounts(counts);
                //6.对其数量进行修改
                branchGoodsDao.updateCounts(inLibrayByNow);
            } else {
                //对其进行添加
                BranchGoods branchGoods = new BranchGoods();
                branchGoods.setCounts(num);
                branchGoods.setBranchid(item.getBranchid());
                branchGoods.setGoodsid(item.getGoodsid());
                branchGoodsDao.addBranchGoods(branchGoods);
            }
        }
        return Result.success();
    }

    /**
     * @param branchAllGoodsVos
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 添加分店商品信息
     * @create time: 2023/9/28 10:20
     */
    @Override
    @Transactional
    public Result addBranchGoods(List<BranchAllGoodsVo> branchAllGoodsVos) {
        //1.获取session
        Staff staff = (Staff) session.getAttribute("staff");
        //2.拿到分店id
        Integer branchid = staff.getBranchid();
        /*判断该分店是否有该商品*/
        for (BranchAllGoodsVo branchAllGoodsVo : branchAllGoodsVos) {
            Goods goods = branchAllGoodsVo.getGoods();
            BranchGoods branchGoods = new BranchGoods();
            branchGoods.setGoodsid(goods.getGoodsid());
            branchGoods.setBranchid(branchid);
            //判断是否存在该商品
            List<BranchGoods> oneInfo = branchGoodsDao.findOneInfo(branchGoods);
            if (oneInfo.size() > 0) {
                return new Result(202, "该分店有该商品", null);
            } else {
                //设置数量为0
                branchGoods.setCounts(0);
                //进行添加分店商品
                branchGoodsDao.addBranchGoods(branchGoods);
            }
        }
        return new Result(200, "添加商品成功", null);
    }
}